package com.cssw.fyzb.busi.api.video;

import com.cssw.fyzb.busi.cache.Cache;
import com.cssw.fyzb.busi.cache.CacheKeys;
import com.cssw.fyzb.busi.cache.Fetcher;
import com.cssw.fyzb.busi.model.tps.UploadSigVo;
import com.cssw.fyzb.busi.service.video.VideoService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Property;
import com.ydn.appserver.annotations.Result;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 添加视频
 *
 * @author Feng Chenparameters
 */
@Function(description = "获取视频上传签名", parameters = {
}, result = @Result(properties = {
        @Property(name = "signature", type = Type.Integer, description = "签名"),
}))
@Component
public class GetVideoUploadSig implements Action {

    @Autowired
    private VideoService videoService;

    private static final Logger logger = LoggerFactory.getLogger(GetVideoUploadSig.class);

    @Override
    public Response execute(Request request) throws Exception {
        try {

            UploadSigVo vo = Cache.get(CacheKeys.uploadSigKey(), new Fetcher<UploadSigVo>() {
                @Override
                public UploadSigVo fetch() {
                    return videoService.getUploadSigVo();
                }
            });

            if ((System.currentTimeMillis() / 1000) > vo.getExpireTimeStamp()) {
                vo = Cache.get(CacheKeys.uploadSigKey(), new Fetcher<UploadSigVo>() {
                    @Override
                    public UploadSigVo fetch() {
                        return videoService.getUploadSigVo();
                    }
                }, true);
            }

            return Response.success().put("data", vo);
        } catch (Exception e) {
            logger.info("", e);
            return Response.fail("操作异常");
        }
    }

}
